package com.qumitech.visualnovel.service.config.impl;

import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.component.util.HttpInvokeUtil;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.UrlConstant;
import com.qumitech.visualnovel.pubdef.dto.config.ConfigBusinessSwitchUpdateDTO;
import com.qumitech.visualnovel.pubdef.dto.config.ConfigParamUpdateDTO;
import com.qumitech.visualnovel.pubdef.dto.config.ConfigTypeDTO;
import com.qumitech.visualnovel.service.config.ConfigService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class ConfigServiceImpl implements ConfigService {
    private static final String BaseConfigUrl = "/admin/config";

    private static final String PUBLICPOOL_URL = "/admin/poundegg/publicpool";
    private static final String SUPREME_EGG_PUBLICPOOL_URL = "/admin/supremeegg/publicpool";

    @Override
    public BaseRsp listParamDTO() {
        String url = UrlConstant.SERVER_URL + BaseConfigUrl + "/param";
        String body = HttpInvokeUtil.httpGet(url, new HashMap<>(), null);
        return JSONObject.parseObject(body, BaseRsp.class);
    }

    @Override
    public BaseRsp updateParam(String key, ConfigParamUpdateDTO updateDTO) {
        String url = UrlConstant.SERVER_URL + BaseConfigUrl + "/param/update";
        String req = JSONObject.toJSONString(updateDTO);
        String body = HttpInvokeUtil.httpPutJson(url, req, UrlConstant.DefaultEncode, null);
        return JSONObject.parseObject(body, BaseRsp.class);
    }

    @Override
    public BaseRsp listBusinessSwitchDTO() {
        String url = UrlConstant.SERVER_URL + BaseConfigUrl + "/switch";
        String body = HttpInvokeUtil.httpGet(url, new HashMap<>(), null);
        return JSONObject.parseObject(body, BaseRsp.class);
    }

    @Override
    public BaseRsp updateBusinessSwitch(Integer businessType, ConfigBusinessSwitchUpdateDTO updateDTO) {
        String url = UrlConstant.SERVER_URL + BaseConfigUrl + "/switch/" + businessType;
        String req = JSONObject.toJSONString(updateDTO);
        String body = HttpInvokeUtil.httpPutJson(url, req, UrlConstant.DefaultEncode, null);
        return JSONObject.parseObject(body, BaseRsp.class);
    }

    @Override
    public BaseRsp listConfigType() {
        String url = UrlConstant.SERVER_URL + BaseConfigUrl + "/reload";
        String body = HttpInvokeUtil.httpGet(url, new HashMap<>(), null);
        return JSONObject.parseObject(body, BaseRsp.class);
    }

    @Override
    public BaseRsp reloadConfig(ConfigTypeDTO configTypeDTO) {
        String url = UrlConstant.SERVER_URL + BaseConfigUrl + "/reload";
        String req = JSONObject.toJSONString(configTypeDTO);
        String body = HttpInvokeUtil.httpPostJson(url, req, UrlConstant.DefaultEncode, null);
        return JSONObject.parseObject(body, BaseRsp.class);
    }

    @Override
    public BaseRsp getPublicPool() {
        String url = UrlConstant.SERVER_URL + PUBLICPOOL_URL;
        JSONObject o = new JSONObject();
        Map<String, Object> req = JSONObject.toJavaObject(o, Map.class);
        String body = HttpInvokeUtil.httpGet(url, req);
        return JSONObject.parseObject(body, BaseRsp.class);
    }

    @Override
    public BaseRsp replacePublicPool(double publicPool) {
        String url = UrlConstant.SERVER_URL + PUBLICPOOL_URL + "/" + publicPool;
        String body = HttpInvokeUtil.httpPutJson(url, new JSONObject(), UrlConstant.DefaultEncode, null);
        return JSONObject.parseObject(body, BaseRsp.class);
    }

    @Override
    public BaseRsp getSupremeEggPublicPool() {
        String url = UrlConstant.SERVER_URL + SUPREME_EGG_PUBLICPOOL_URL;
        JSONObject o = new JSONObject();
        Map<String, Object> req = JSONObject.toJavaObject(o, Map.class);
        String body = HttpInvokeUtil.httpGet(url, req);
        return JSONObject.parseObject(body, BaseRsp.class);
    }

    @Override
    public BaseRsp replaceSupremeEggPublicPool(double publicPool) {
        String url = UrlConstant.SERVER_URL + SUPREME_EGG_PUBLICPOOL_URL + "/" + publicPool;
        String body = HttpInvokeUtil.httpPutJson(url, new JSONObject(), UrlConstant.DefaultEncode, null);
        return JSONObject.parseObject(body, BaseRsp.class);
    }
}
